import { compileMDX } from "next-mdx-remote/rsc";
import { serialize } from "next-mdx-remote/serialize";
import rehypeKatex from "rehype-katex";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkSmartypants from "remark-smartypants";
import { rehypeSyntaxHighlighting } from "../plugins/index.js";
export const getCompiledMdx = async ({ source, mdxOptions, scope, parseFrontmatter = true, }) => {
    try {
        const serializedResponse = await serialize(source, {
            mdxOptions: {
                remarkPlugins: [
                    remarkGfm,
                    remarkSmartypants,
                    remarkMath,
                    ...(mdxOptions?.remarkPlugins || []),
                ],
                rehypePlugins: [
                    rehypeKatex,
                    [
                        rehypeSyntaxHighlighting,
                        {
                            ignoreMissing: true,
                        },
                    ],
                    ...(mdxOptions?.rehypePlugins || []),
                ],
                format: mdxOptions?.format || "mdx",
                useDynamicImport: mdxOptions?.useDynamicImport || true,
            },
            scope,
            parseFrontmatter,
        });
        return serializedResponse;
    }
    catch (error) {
        console.error(`Error occurred while serializing MDX: ${error}`);
        throw error;
    }
};
export const getCompiledServerMdx = async ({ source, mdxOptions, components, parseFrontmatter = true, }) => {
    return await compileMDX({
        source,
        options: {
            mdxOptions: {
                remarkPlugins: [
                    remarkGfm,
                    remarkSmartypants,
                    remarkMath,
                    ...(mdxOptions?.remarkPlugins || []),
                ],
                rehypePlugins: [
                    rehypeKatex,
                    [
                        rehypeSyntaxHighlighting,
                        {
                            ignoreMissing: true,
                        },
                    ],
                    ...(mdxOptions?.rehypePlugins || []),
                ],
                format: mdxOptions?.format || "mdx",
                useDynamicImport: mdxOptions?.useDynamicImport || true,
            },
            parseFrontmatter,
        },
        components,
    });
};
